<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\base\Systrade;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7
 * Time: 10:19
 */
class Trade extends Controller{
    /*****************旅客乘车订单*************************/
    /*
     * 数据查询
     */
    public function ListItem(){
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        _Db::Validates($request_data);
        $param_page = input('Page', 1);
        $where = '1=1';
        $where_count = '1=1';
        $array_data_range = explode('|', $get_keyword->text);
        if(!empty($get_keyword->text)){
            if (is_array($array_data_range) && $array_data_range[0] !== '' && $array_data_range[1] !== '') {
                $where .= " and pay_time between '" . $array_data_range[0] . " 00:00:00'" . " and '" . $array_data_range[1] . " 23:59:59'";
                $where_count .= " and oper_date between '" . $array_data_range[0] . " 00:00:00'" . " and '" . $array_data_range[1] . " 23:59:59'";
            }
        }
        $data = Systrade::DayItem($where,$where_count,$param_page);
        _Db::ResponseJson($data);
    }
    /*
     * 本天
     * 按条查
     * 每天列表
     */
    public function FindDay(){
        $param_page = input('Page', 1);
        $day_time = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $where = "pay_time between '" . date('Y-m-d',$day_time) . " 00:00:00'" . " and '" . date('Y-m-d',$day_time) . " 23:59:59'";
        $where_count = "oper_date between '" . date('Y-m-d',$day_time) . " 00:00:00'" . " and '" . date('Y-m-d',$day_time) . " 23:59:59'";
        $data = Systrade::DayItem($where,$where_count,$param_page);
        _Db::ResponseJson($data);
    }
    /*
     * 本周
     * 按天查
     * 一周列表
     */
    public function FindWeek(){
        //$week  = date("Y-m-d",strtotime("+7 day",strtotime($get_keyword->text)));
        $param_page = input('Page', 1);
        $timestamp = time();
        $start_time = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
        $end_time = strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
        $where = "pay_time between '" . date('Y-m-d',$start_time) . " 00:00:00'" . " and '" . date('Y-m-d',$end_time) . " 23:59:59'";
        $where_count = "oper_date between '" . date('Y-m-d',$start_time) . " 00:00:00'" . " and '" . date('Y-m-d',$end_time) . " 23:59:59'";
        $data = Systrade::DayItem($where,$where_count,$param_page);
        _Db::ResponseJson($data);
    }
    /*
     * 本月
     * 按天查
     * 每天总数
     */
    public function FindMonth(){
        $param_page = input('Page', 1);
        $start_time = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $end_time = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $where = "pay_time between '" . date('Y-m-d',$start_time) . " 00:00:00'" . " and '" . date('Y-m-d',$end_time) . " 23:59:59'";
        $where_count = "oper_date between '" . date('Y-m-d',$start_time) . " 00:00:00'" . " and '" . date('Y-m-d',$end_time) . " 23:59:59'";
        $data = Systrade::WeekItem($where,$where_count,$param_page);
        _Db::ResponseJson($data);
    }
    /*
     * 本年
     * 按月查
     * 每月总数
     */
    public function FindYear(){
        $param_page = input('Page', 1);
        $start_time = date('Y-m',mktime(0, 0, 0, 1, 1, date('Y')));
        $end_time = date('Y-m',mktime(23, 59, 59, 12, 31, date('Y')));
        $where = "DATE_FORMAT(pay_time, '%Y-%m') between '" . $start_time . "' and '" . $end_time . "'";
        $where_count = "DATE_FORMAT(oper_date, '%Y-%m') between '" . $start_time . "' and '" . $end_time . "'";
        $data = Systrade::YearItem($where,$where_count,$param_page);
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     * 每天列表
     * FindMonth->ItemDay
     */
    public function ItemDay(){
        $request_data = [
            $post_day = new RequestEntity('Day', '日期', false, 0),
        ];
        $param_page = input('Page', 1);
        _Db::Validates($request_data);
        if(!empty($post_day->text!='')){
            $where = "pay_time between '" . $post_day->text . " 00:00:00'" . " and '" . $post_day->text . " 23:59:59'";
            $where_count = "oper_date between '" . $post_day->text . " 00:00:00'" . " and '" . $post_day->text . " 23:59:59'";
        }else{
            $where = '1=1';
            $where_count = '1=1';
        }
        $data = Systrade::DayItem($where,$where_count,$param_page);
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     * 每条
     * FindDay/FindWeek->ItemInfo
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0),
        ];
        _Db::Validates($request_data);
        $param_where = [
            'a.order_id' => $post_itemid->text,
        ];
        $result = Db::table('order_info')
            ->field(
                [
                    'a.order_id'	     =>'Id',            //订单ID
                    'a.out_trade_no'	 =>'OutTradeNo',    //订单流水号
                    'a.pay_time'	     =>'PayTime',       //付款时间
                    'a.pay_money'	     =>'PayMoney',      //实付金额
                    'a.return_cache'	 =>'ReturnCache',   //微信支付对账单
                    'a.invoice_message'	 =>'InvoiceMessage',//电子发票信息
                    'b.nick_name'        =>'NickName',      //用户
                    'c.type_name'        =>'InvoiceState',  //电子发票状态
                    'd.type_name'        =>'InvoiceType',
                    'e.type_name'        =>'TradeState',    //订单状态
                    'h.line_from'        =>'LineFrom',
                    'h.line_destination' =>'LineDes'
                ]
            )
            ->alias('a')
            ->join('user b','a.openid = b.openid','LEFT')
            ->join('sys_public_type c','a.invoice_state = c.type_id and c.type_classify = "发票状态"','LEFT')
            ->join('sys_public_type d','a.invoice_type = d.type_id and d.type_classify = "发票类型"','LEFT')
            ->join('sys_public_type e','a.trade_state = e.type_id and e.type_classify = "订单状态"','LEFT')
            ->join('line h','a.line_id = h.line_id','LEFT')
            ->where($param_where)
            ->find();
        //echo Db::table('')->getLastSql();
        _Db::ResponseJson($result);
    }
}